package com.ericsson.entironment.interfaces;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ericsson.entironment.interfaces.receiver.MessageReceiver;
import com.ericsson.entironment.util.ReceiverBean;
import com.ericsson.entironment.util.SysParams;

public class InterfaceMain extends HttpServlet
{
	private static Log log=LogFactory.getLog(InterfaceMain.class);
	/**
	 * Constructor of the object.
	 */
	public InterfaceMain()
	{
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy()
	{
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}
	/**
	 * server启动时初始化，用于接收和终端之间的消息
	 *
	 * @throws ServletException if an error occure
	 */
	public void init() throws ServletException
	{
		log.info("\n------------- the main thread of the interface starting -----------------\n");
		//启动所有的消息接收器
		int i;
		MessageReceiver receiver;
		Vector<ReceiverBean> vcts=SysParams.getInstance().getMsgReceiver();
		for(i=0;i<vcts.size();i++)
		{
			try
			{
				if (vcts.get(i).load==1)
				{//启动时加载
					receiver=(MessageReceiver)Class.forName(vcts.get(i).getClassName()).newInstance();
					receiver.init(vcts.get(i).getName());
					Thread a=new Thread(receiver);
					a.start();//启动进程
					if (log.isDebugEnabled())
						log.debug("\nThe message receiver["+vcts.get(i).getClassName()+"] loaded \n");
				}
			}
			catch (ClassNotFoundException e)
			{

				log.fatal("can not load the message receiver["+vcts.get(i).getClassName()+"]\n",e);
				log.info("System halt");
				System.exit(-1);
			}
			catch (InstantiationException e)
			{
				log.fatal("can not load the message receiver["+vcts.get(i).getClassName()+"]\n",e);
				log.info("System halt");
				System.exit(-1);
			}
			catch (IllegalAccessException e)
			{
				log.fatal("can not load the message receiver["+vcts.get(i).getClassName()+"]\n",e);
				log.info("System halt");
				System.exit(-1);
			}
			
			//Thread a=new Thread()
		}
		
		
		log.info("\n------------- the main thread of the interface started -----------------\n");
	}

}
